package cn.edu.ahtcm.springboottest.controller;


import cn.edu.ahtcm.springboottest.service.FaceDetectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class FaceController {
    @Autowired
    private FaceDetectionService faceDetectionService;

    @RequestMapping(value = "/faceDetect/image", method = RequestMethod.POST, produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[] detectFaceImage(@RequestParam("file") MultipartFile file) throws IOException {

        if ( !validateImage(file))
        {
            return new byte[1];
        }

        return faceDetectionService.detectFace(file).toImage();
    }

    private Boolean validateImage(MultipartFile image) {
        return image.getContentType().equals("image/jpeg");
    }

}